package com.example.fp.zlz.controller;

import com.example.fp.cgc.code.logAnno.LogAnno;
import com.example.fp.zlz.entity.Admin;
import com.example.fp.zlz.mapper.AdminMapper;
import io.swagger.annotations.ApiOperation;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 说明: 用户表操作
 * 编写人: 张连壮
 * 编写时间: 2019-12-31
 * 修改时间:
 * 用户登录验证，及相关操作
 */
@CrossOrigin
@RestController
public class AdminController {
	@Autowired
	private AdminMapper adminMapper;


	@LogAnno(operateType = "用户注册")
	@ApiOperation("用户注册")
	@RequestMapping(value = "/insertadmin",method = RequestMethod.POST)
	public int inserteAdmin(@RequestBody JSONObject user){
		Admin admin=new Admin();
		admin.setUsername(user.get("username").toString());
		admin.setPassword(user.get("password").toString());
		int flag=adminMapper.selectadminbyname(admin);
		if (flag!=0) {
			return 0;
		} else {
			admin.setBg(1);
			return adminMapper.insert(admin);
		}
	}

	@RequestMapping(value = "/getUserList",method = RequestMethod.GET)
	public List<Admin> getUserList(){
		return adminMapper.selectList(null);
	}


	@LogAnno(operateType = "修改密码")
	@ApiOperation("修改密码")
	@PostMapping("/update")
	public int updateAdmin(Admin admin){
		return adminMapper.updateById(admin);
	}

	@LogAnno(operateType = "注销账号")
	@ApiOperation("注销账号")
	@DeleteMapping("/delete")
	public int deleteAdmin(Admin admin){
		return adminMapper.deleteById(admin.getId());
	}

	//下面测试中


}
